﻿using System;
using System.Data;
using DotNetNuke.Entities.Content;
using DotNetNuke.Entities.Users;

namespace DotNetNuke.Modules._2DoTasks.Components.Entities
{
    public class _Task : ContentItem
    {
        public int TaskId { get; set; }

        public int PortalId { get; set; }

        public Nullable<int> ProjectId { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public Nullable<int> AssignedUserId { get; set; }

        public Nullable<DateTime> TargetCompletiondate { get; set; }

        public Nullable<DateTime> CompletionDate { get; set; }

        public DateTime Creationdate { get; set; }

        public int CreationUserId { get; set; }

        public int RequestedByUserId { get; set; }

        public DateTime LastChangeDate { get; set; }

        public int LastChangeUserId { get; set; }

        public Int16 TaskType { get; set; }

        public Int16 Priority { get; set; }

        public Int16 TaskStatus { get; set; }

        public string ProjectName { get; set; }

        /// <summary>
        /// The creation user display name
        /// </summary>
        public UserInfo CreatedByUser
        {
            get
            {
                return CreatedByUserID != 0 ? UserController.GetUserById(PortalId, CreatedByUserID) : null;
            }
        }

        /// <summary>
        /// the last change user display name
        /// </summary>
        public UserInfo LastChangeUser
        {
            get
            {
                return LastChangeUserId != 0 ? UserController.GetUserById(PortalId, LastChangeUserId) : null;
            }
        }

        /// <summary>
        /// the user that requested the task display name
        /// </summary>
        public UserInfo RequestedByUser
        {
            get
            {
                return RequestedByUserId != 0 ? UserController.GetUserById(PortalId, RequestedByUserId) : null;
            }
        }

        /// <summary>
        /// The user that to wich the task is assigned
        /// </summary>
        public UserInfo AssignedUser
        {
            get
            {
                return AssignedUserId.HasValue ? UserController.GetUserById(PortalId, AssignedUserId.Value) : null;
            }
        }

        #region IHydratable Implementation

        public override void Fill(IDataReader dr)
        {
            TaskId = Convert.ToInt32(dr["Tsk_TaskId"]);

            ProjectId = dr["Tsk_ProjectId"] as Nullable<int>;

            PortalId = Convert.ToInt32(dr["Tsk_PortalId"]);

            Name = dr["Tsk_Name"].ToString();

            Description = dr["Tsk_Description"].ToString();

            ModuleID = Convert.ToInt32(dr["Tsk_ModuleId"]);

            AssignedUserId = dr["Tsk_AssignedUserId"] as Nullable<int>;

            TargetCompletiondate = dr["Tsk_TargetCompletiondate"] as Nullable<DateTime>;

            CompletionDate = dr["Tsk_CompletionDate"] as Nullable<DateTime>;

            Creationdate = Convert.ToDateTime(dr["Tsk_Creationdate"]);

            CreationUserId = Convert.ToInt32(dr["Tsk_CreationUserId"]);

            RequestedByUserId = Convert.ToInt32(dr["Tsk_RequestedByUserId"]);

            LastChangeDate = Convert.ToDateTime(dr["Tsk_LastChangeDate"]);

            LastChangeUserId = Convert.ToInt32(dr["Tsk_LastChangeUserId"]);

            TaskType = Convert.ToInt16(dr["Tsk_TaskType"]);

            Priority = Convert.ToInt16(dr["Tsk_Priority"]);

            TaskStatus = Convert.ToInt16(dr["Tsk_TaskStatus"]);

            ProjectName = dr["Prj_Name"].ToString();

            //base.FillInternal(dr); ToDo - Implement Taxonomy
        }

        public override int KeyID
        {
            get
            {
                return TaskId;
            }
            set
            {
                TaskId = value;
            }
        }

        #endregion IHydratable Implementation
    }
}